package com.smarthome.service;

import com.smarthome.model.Device;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceService {

    // 模拟设备存储
    private final Map<String, Device> devices = new HashMap<>();

    public DeviceService() {
        // 初始化一些示例设备
        devices.put("light1", new Device("light1", "客厅灯", "light", false, "亮度: 70%"));
        devices.put("thermo1", new Device("thermo1", "卧室 thermostat", "thermostat", true, "温度: 24°C"));
        devices.put("door1", new Device("door1", "前门", "door", false, "锁定状态"));
    }

    // 获取所有设备
    public List<Device> getAllDevices() {
        return new ArrayList<>(devices.values());
    }

    // 根据ID获取设备
    public Device getDeviceById(String id) {
        return devices.get(id);
    }

    // 切换设备状态
    public Device toggleDeviceStatus(String deviceId) {
        Device device = devices.get(deviceId);
        if (device != null) {
            device.setStatus(!device.isStatus());
        }
        return device;
    }

    // 更新设备信息（如温度、亮度）
    public Device updateDeviceInfo(String deviceId, String info) {
        Device device = devices.get(deviceId);
        if (device != null) {
            device.setAdditionalInfo(info);
        }
        return device;
    }
}
    